var express  = require( 'express' ),
    app      = express(),
    _dir     = __dirname + '/webapp',
    proxy    = require( 'express-http-proxy' ),
    partials = require( 'express-partials' ),
    path     = require( 'path' ),
    config   = require( _dir + '/config/'+( process.env.config?process.env.config:'config' ) ),
    locals   = require( _dir + '/config/locals' )( app ),
    routes   = require( _dir + '/routes/' );

//   配置参数信息
app.set( 'config', config );

//   服务端口号设置
app.set( 'port', config.port );
//app.set('view option',{layout:true});

//   视图层目录设置
app.set('views', path.join( _dir, config.views ));

//   静态资源目录设置
app.use( express.static( _dir + config.static ) );

//   模板引擎设置为ejs
app.set( 'view engine', config.engine );

//    是否缓存设置 缓存请求头
app.set( 'view cache', config.cache );

//    设置layout载入功能 注意放置得靠前一点
app.use( partials() );

//   路由设置
routes({
    express: express,
    app: app,
    routes: express.Router(),
    originProxy: proxy,
    helper: require( _dir+'/helper/' )
});

//   服务端口的监听
app.listen( app.get( 'port' ), function(){
    
    console.log( '\033[32mhttp服务 ---> http://localhost:'+ app.get( 'port' ) +', 已启动成功^_^\033[0m' );

});